package com.ruoyi.web.controller.construction;

import java.io.IOException;
import java.util.List;
import java.util.Map;

import com.ruoyi.common.config.RuoYiConfig;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.utils.ServletUtils;
import com.ruoyi.common.utils.file.FileUploadUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.web.service.TokenService;
import com.ruoyi.system.domain.construction.ConstructionPicInfo;
import com.ruoyi.system.service.construction.ConstructionPicInfoService;
import com.ruoyi.system.utils.FileUtil;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 施工队图片信息Controller
 *
 * @author 张少丛
 * @date 2020-11-04
 */
@RestController
@RequestMapping("/construction/picInfo")
public class ConstructionPicInfoController extends BaseController
{
    @Autowired
    private ConstructionPicInfoService constructionPicInfoService;

    @Autowired
    private TokenService tokenService;

    /**
     * 查询施工队图片信息列表
     */
    @PreAuthorize("@ss.hasPermi('construction:picInfo:list')")
    @GetMapping("/list")
    public TableDataInfo list(ConstructionPicInfo constructionPicInfo)
    {
        startPage();
        List<ConstructionPicInfo> list = constructionPicInfoService.selectConstructionPicInfoList(constructionPicInfo);
        return getDataTable(list);
    }

    /**
     * 导出施工队图片信息列表
     */
    @PreAuthorize("@ss.hasPermi('construction:picInfo:export')")
    @Log(title = "施工队图片信息", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(ConstructionPicInfo constructionPicInfo)
    {
        List<ConstructionPicInfo> list = constructionPicInfoService.selectConstructionPicInfoList(constructionPicInfo);
        ExcelUtil<ConstructionPicInfo> util = new ExcelUtil<ConstructionPicInfo>(ConstructionPicInfo.class);
        return util.exportExcel(list, "info");
    }

    /**
     * 获取施工队图片信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('construction:picInfo:query')")
    @GetMapping(value = "/{constructionPicId}")
    public AjaxResult getInfo(@PathVariable("constructionPicId") Integer constructionPicId)
    {
        return AjaxResult.success(constructionPicInfoService.selectConstructionPicInfoById(constructionPicId));
    }

    /**
     * 新增施工队图片信息
     */
    @PreAuthorize("@ss.hasPermi('construction:picInfo:add')")
    @Log(title = "施工队图片信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ConstructionPicInfo constructionPicInfo)
    {
        return toAjax(constructionPicInfoService.insertConstructionPicInfo(constructionPicInfo));
    }

    /**
     * 修改施工队图片信息
     */
    @PreAuthorize("@ss.hasPermi('construction:picInfo:edit')")
    @Log(title = "施工队图片信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ConstructionPicInfo constructionPicInfo)
    {
        return toAjax(constructionPicInfoService.updateConstructionPicInfo(constructionPicInfo));
    }

    /**
     * 删除施工队图片信息
     */
    @PreAuthorize("@ss.hasPermi('construction:picInfo:remove')")
    @Log(title = "施工队图片信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{constructionPicIds}")
    public AjaxResult remove(@PathVariable Integer[] constructionPicIds)
    {
        return toAjax(constructionPicInfoService.deleteConstructionPicInfoByIds(constructionPicIds));
    }


    /**
     * 上传图片
     * @param file
     * @return
     */
    @PostMapping("/uploadImage")
    public Map<String,Object> up(MultipartFile file){
        String url = "/home/ruoyi/profile/xiazai/";
        return FileUtil.uploadImage(file,url);
    }

    @PostMapping("/uploadPicture")
    public AjaxResult uploadNewsAdvert(@RequestParam("picture") MultipartFile file) throws IOException {
        if (!file.isEmpty()) {
            String fileName = FileUploadUtils.upload(RuoYiConfig.getAvatarPath(),file);
            String s = "http://51jhf.com" + fileName;
            return AjaxResult.success(s);
        }
        return AjaxResult.error("上传图片异常，请联系管理员");
    }

}
